import { Loger } from "../loger/Loger";

/**发送类型 */
export enum HttpSendMethod{
    GET = "GET",
    POST = "POST"
}

export default class HttpUtils {
    /**发送http get 请求*/
    public static get(url: string, headers?: { [key: string]: string }, callback?: (data: any) => void, timeout: number = 10000) {
        this.send(HttpSendMethod.GET, url, null, headers, callback, timeout);
    }
    /**发送http post 请求*/
    public static post(url: string, body: any, headers?: { [key: string]: string }, callback?: (data: any) => void, timeout: number = 10000) {
        this.send(HttpSendMethod.POST,url,body,headers,callback,timeout);
    }

    public static send(method: HttpSendMethod, url: string, body?: any, headers?: { [key: string]: string }, callback?: (data: any) => void, timeout: number = 10000) {
        let request = new XMLHttpRequest();
        request.open(method, url, true);
        request.timeout = timeout
        request.setRequestHeader("Access-Control-Allow-Origin", "*")
        request.setRequestHeader("Access-Control-Allow-Headers", "*")

        if (headers) {
            request.setRequestHeader("Content-Type", "application/json;charset=utf-8")
            // request.setRequestHeader("Access-Control-Allow-Origin", "*")
            // request.setRequestHeader("Access-Control-Allow-Headers", "*")
            for (const key in headers) {
                if (headers.hasOwnProperty(key)) {
                    const value = headers[key];
                    // Log.log(key+" :=== "+ value);
                    request.setRequestHeader(key, value)
                }
            }
        } else {
            request.setRequestHeader("Content-Type", "application/json;charset=utf-8")
            // request.setRequestHeader("Access-Control-Allow-Origin", "*")
            // request.setRequestHeader("Access-Control-Allow-Headers", "*")
            // request.setRequestHeader("Cache-Control","no-cache")
        }

        request.ontimeout = (ev) => {
            Loger.log('=======http_get: request time out.....');
            callback({ timeout: ev });
        };
        request.onabort = (ev) => {
            Loger.log('=======http_get: request onabort......');
            callback({ abort: ev });
        }
        request.onerror = (ev) => {
            Loger.log('=======http_get: request onerror......');
            callback(404);
        }
        request.onreadystatechange = function () {
            // Log.log("===request.readyState = ", request.readyState + " request.status="+request.status)
            if (request.readyState === 4) {
                if (request.status >= 200 && request.status < 300) {

                    let response = request.responseText;
                    Loger.log(`===finish ${method} : ${url}`)
                    if (response != '' && response != 'OK') {
                        let responseJson = JSON.parse(response)
                        Loger.log("==responseJson :", response)
                        callback(responseJson)
                    } else {
                        callback(null)
                    }
                } else if (request.status === 401 || request.status === 404) {
                    Loger.log(`==finish ${method} ${request.status}: ${url}`)
                    callback(request.status)
                }
            }
        }
        Loger.log(`===begin ${method} : ${url}`)
        if (body) {
            const postData = JSON.stringify(body)
            Loger.log(`===post body : ${postData}`)
            request.send(postData)
        } else {
            request.send()
        }
    }

}